<?php
	require_once ("../../config.inc.php");
	require_once ("../../include/mysql.inc.php");
	require_once ("../../include/article.inc.php");
	
	require_once ("../../util/stringUtil.inc.php");
	
	$articleDao = new Article();
	
	$rows = $articleDao->getLatestN(ARTICLE_RSS_SIZE);

	$dom = new DOMDocument("1.0","UTF-8");
	$rss = $dom->createElement("rss");
	$rss->setAttribute('version', '2.0');
	$rssRoot = $dom->createElement("channel");
	
	
	$title = $dom->createElement('title', 'HomePage');
	$description = $dom->createElement('description', '王小为的技术博客');
	$link = $dom->createElement('link', 'http://wzwahl36.net');
	$lastBuildDate = $dom->createElement('lastBuildDate', date('Y-m-d H:i:s'));
	$pubDate = $dom->createElement('pubDate', date('Y-m-d H:i:s'));
	$ttl = $dom->createElement('ttl', 1800);
	$rssRoot->appendChild($title);
	$rssRoot->appendChild($description);
	$rssRoot->appendChild($link);
	$rssRoot->appendChild($lastBuildDate);
	$rssRoot->appendChild($pubDate);
	$rssRoot->appendChild($ttl);
	foreach ($rows as $row) {
		$item = $dom->createElement('item');

		$title = $dom->createElement('title', $row[title]);
		$description = strip_tags($row[content]);
		$description = htmlspecialchars(StringUtil::chinasuber(strip_tags(htmlspecialchars_decode($description)), INDEX_ARTICLE_CONTENT_LENGTH));
		$description = $dom->createElement("content", $description);
		$link = $dom->createElement('link', 'http://wzwahl36.net/blog.php?id=' . $row['id']);
		$guid = $dom->createElement('guid', $row['id']);
		$pubDate = $dom->createElement("pubDate", $row[time]);
		
		$item->appendChild($title);
		$item->appendChild($description);
		$item->appendChild($link);
		$item->appendChild($guid);
		$item->appendChild($pubDate);
		
		$rssRoot->appendChild($item);
	}
	
	$rss->appendChild($rssRoot);
	$dom->appendChild($rss);
	header("Content-type:text/xml");
    $xmlfile = $dom->saveXML();
    file_put_contents("../../xml_data/articleRss.xml",$xmlfile);
    echo $xmlfile;
	
?>